package com.itcast.ceph.demo.startup;

import com.itcast.ceph.demo.service.CephOperator;
import com.itcast.ceph.demo.service.SwiftOperator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.itcast"})
public class CephDemoApplication {
    public static void main(String[] args) throws Exception{
        //radosApi(args);
        swiftApi();

    }

    public static void radosApi(String[] args){
        System.out.println("start....");
        String username = "admin";
        String monIp = "192.168.52.11:6789;192.168.52.12:6789;192.168.52.13:6789";
        String userKey = "AQDsy3Bkmbd3IhAAtz1Q5+5YpXm8eHVlbir+qw==";
        String mountPath = "/";
        CephOperator cephOperate = null;
        try {
            String opt = (args == null || args.length < 1)? "" : args[0];
            cephOperate = new CephOperator(username, monIp, userKey, mountPath);
            if("upload".equals(opt)) {
                cephOperate.uploadFileByPath("/root/temp_upload_fs", args[1]);
            }else if("download".equals(opt)) {
                cephOperate.downloadFileByPath("/root/temp_download_fs", args[1]);
            }else {
                System.out.println("Unrecognized Command! Usage  opt[upload|download] filename[path]!");
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(null != cephOperate) {
                cephOperate.umount();
            }
        }
        System.out.println("end....");
    }

    //
    public static void swiftApi() throws Exception{
        //1.打印列出用户的所有容器信息
        ConfigurableApplicationContext appContext = SpringApplication.run(CephDemoApplication.class);
        SwiftOperator swiftOperator = appContext.getBean(SwiftOperator.class);
        swiftOperator.lisContainer();
        //2.上传指定的文件

        String objName = "test_ceph";
        swiftOperator.createObject(objName,"E:\\test_swift.txt");

        //3、从Ceph下载文件到指定路径
        swiftOperator.retrieveObject(objName,"E:\\test_download.txt");

        System.out.println("处理完成");
    }
}
